# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2010  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


from PyQt4.QtGui import *
from PyQt4 import QtCore

from common import elementSymbols, elementColors

import openbabel


class Atom(QGraphicsItem):

	font = QFont()
	_fontMetrics = QFontMetrics(font)
	fontHeight = _fontMetrics.height()

	def __init__(self, atom, parent=None):
		self.selected = False
		
		QGraphicsItem.__init__(self, parent)

		self.atom = atom
		self.symbol = elementSymbols[self.atom.GetAtomicNum()]
		#if self.atom.ImplicitHydrogenCount() > 0:
			#self.symbol += "H"+str(self.atom.ImplicitHydrogenCount())
		#print "Adding atom:", atom.GetIdx()

		self.updatePos()

		self.hover = False

		self.setFlag(QGraphicsItem.ItemIsMovable, True)
		self.setFlag(QGraphicsItem.ItemIsSelectable, True)
		try:
			self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
		except AttributeError:
			pass #Need this in Qt4.6 not in Qt4.5

		self.setZValue(10)

	def setSelected(self, selected):
		self.selected = selected
		self.update()

	def setAtomicNum(self, num):
		oldNum = self.atom.GetAtomicNum()
		self.atom.SetAtomicNum(num)
		self.updateSymbol()
		self.update()
		return oldNum

	def setAtom(self, atom):
		self.atom = atom
		self.updateSymbol()

	def updateSymbol(self):
		self.symbol = elementSymbols[self.atom.GetAtomicNum()]
		#if self.atom.ImplicitHydrogenCount() > 0:
			#self.symbol += "H"+str(self.atom.ImplicitHydrogenCount())
		
		self.prepareGeometryChange()
		self.update()

	def updatePos(self):
		self.setPos(self.parentItem().translateFromAtomCoords([self.atom.GetX(), self.atom.GetY()]))

	def itemChange(self, change, value):
		if change == QGraphicsItem.ItemPositionHasChanged:
			value = value.toPointF()
			self.atom.SetVector(self.parentItem().translateToAtomCoords(value))
			self.parentItem().updateBonds(self.atom)
		return QGraphicsItem.itemChange(self, change, value)

	def boundingRect(self):
		return QtCore.QRectF(-self._fontMetrics.width(self.symbol)/2.0, -self.fontHeight/2.0, self._fontMetrics.width(self.symbol), self.fontHeight)

	def paint(self, painter, option, widget):
		#print "drawing atom:", self.atom.GetIdx()
		if self.selected:
			painter.drawRect(self.boundingRect())
		color = elementColors[elementSymbols[self.atom.GetAtomicNum()]]
		painter.setPen(QColor(color[0]*255, color[1]*255, color[2]*255))
		painter.drawText(QtCore.QPointF(-self._fontMetrics.width(self.symbol)/2.0, self.fontHeight*.3), self.symbol)
